package com.ssm.controller;

import com.ssm.pojo.Employee;
import com.ssm.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
public class EmployeeController {
  @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/employee",method = RequestMethod.GET)
    public String selectAll(Model model){
        List<Employee> list=employeeService.getAll();
        System.out.println(list);
        model.addAttribute("list",list);
        return "employee_list";
    }
    @RequestMapping(value = "/employee",method = RequestMethod.POST)
    public String insertByEmployee(Employee employee){
        employeeService.insertEmployee(employee);
        return "sucess";
    }


    @RequestMapping(value = "/employee",method = RequestMethod.PUT)
    public String toAlterAfter(Employee employee){
        employeeService.update(employee);
        return "redirect:employee";
    }

    @RequestMapping(value = "/employee/{empId}",method = RequestMethod.GET)
    public String selectById(Model model,@PathVariable("empId") Integer empId){
        Employee employee = employeeService.getById(empId);
        model.addAttribute("employee",employee);
        return "employee_update";
    }

    @RequestMapping(value = "/aa")
    public String goAdd(){
        return "employee_add";
    }

    @RequestMapping(value = "/employee/{empId}",method = RequestMethod.DELETE)
    public String deleteById(@PathVariable("empId") Integer empId){
        employeeService.deleteById(empId);
        return "redirect:/employee";
    }
}
